﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using log4net;
using CashFlow.Db.Entity;
using SysAdmin._02_Common;
using SysAdmin._03_Service._00_Db;
using SysAdmin._04_Form._00_Common;

namespace SysAdmin._04_Form._04_Fee
{
    public partial class FeeForm : Form
    {
        private static ILog _logger = LogManager.GetLogger(typeof(FeeForm));
        private int _index = 0;

        public FeeForm()
        {
            InitializeComponent();
        }

        private void FeeForm_Load(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;
        }

        private void FeeForm_Shown(object sender, EventArgs e)
        {
            dataGridView1.Height = Height - (Padding.Top + Padding.Bottom) - panel1.Height - 10;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string date = null;
                if (dateTimePicker1.Checked)
                {
                    date = dateTimePicker1.Value.ToString("yyyy-MM-dd");
                }
                string name = textBox1.Text.Trim();
                var ls = FeeService.QueryByWhere(date, name);
                var ls2 = ls.Select(x => FeeAndUserInfoView_2_DataGridItemData(x)).ToList();
                _index = 0;
                dataGridView1.DataSource = ls2;
            }
            catch (Exception ex)
            {
                _logger.Error(ex);
                UICommonTool.ShowAlertDialog(ex.Message);
            }
        }

        private DataGridItemData FeeAndUserInfoView_2_DataGridItemData(FeeAndUserInfoView data)
        {
            var data2 = new DataGridItemData();

            data2.No = (++_index).ToString().PadLeft(5, '0');
            data2.Name = data.UserInfo.Name;
            data2.ProfitFee = MyCommonTool.ToString(data.Fee.OrderProfitFee);
            data2.ShopManagerFee = MyCommonTool.ToString(data.Fee.ShopManagerManageFee);
            data2.BoundFee = MyCommonTool.ToString(data.Fee.BoundFee);
            data2.Date = data.Fee.Date;
            data2.ConsignmentSaleFee = MyCommonTool.ToString(data.Fee.ConsignmentSaleFee);

            return data2;
        }

        private class DataGridItemData
        {
            public string No { get; set; }
            public string Name { get; set; }
            public string Date { get; set; }
            public string ProfitFee { get; set; }
            public string ShopManagerFee { get; set; }
            public string BoundFee { get; set; }
            public string ConsignmentSaleFee { get; set; }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var form = new GenerateFeeForm();
            form.ShowDialog();
        }
    }
}
